﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "AboutQtDialog.h"
#include <QFile>
#include <QVBoxLayout>
#include <QLabel>
#include <QDesktopServices>
#include <QUrl>
#include <QProcess>
#include <QStringList>
#include <QCoreApplication>
#include <QSettings>
#include <QTextCodec>
#include <QStringList>
#include <QDialog>
#include "DebugMessage.h"
#include "GbmpNew.h"
#include "GbmpFileSystem.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

AboutQtDialog::AboutQtDialog(const QString &qtVersion, QWidget *parent, const QString &language)
    : QDialog(parent)
{
    setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
    setObjectName(QStringLiteral("AboutQtForm"));
    InitDlg(qtVersion,language);
}

AboutQtDialog::~AboutQtDialog()
{
}

void AboutQtDialog::GetConfigFilePath(QString &sFilePath)
{
    sFilePath = QCoreApplication::applicationDirPath() + QString("/QtLicense");
}

void AboutQtDialog::InitDlg(const QString &qtVersion, const QString &language)
{
    QString exePath;
    GetConfigFilePath(exePath);
    QSettings settings(exePath + "/Qt_LGPLLicense.ini", QSettings::IniFormat, this);
    settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
    QString commonSectionName = "Common_" + language;

    QString titleText = settings.value(commonSectionName + "/Title", "About Qt").toString();
    setWindowTitle(titleText);

    QVBoxLayout *layout = NEW_AS_QT_CHILD(QVBoxLayout, this);
    DBG_WARN_AND_RETURN_VOID_UNLESS(layout, L"layout为空。",L"GDMPLab",L"2024-03-30");

    // About
    QLabel *noticeLabel = NEW_AS_QT_CHILD(QLabel, this);
    DBG_WARN_AND_RETURN_VOID_UNLESS(noticeLabel, L"noticeLabel为空。",L"GDMPLab",L"2024-03-30");
    noticeLabel->setObjectName(QStringLiteral("labelMainText"));
    noticeLabel->setText(settings.value(commonSectionName + "/LicenseText").toString());
    noticeLabel->setWordWrap(true);
    layout->addWidget(noticeLabel);
    
    // Qt Source
    QString sourceText = settings.value(commonSectionName + "/SrcText").toString();
    QString urlKey("5.6.3/URL");
    if (!qtVersion.isEmpty() && settings.contains(qtVersion + "/URL"))
        urlKey = qtVersion + "/URL";
    sourceText = sourceText.arg(settings.value(urlKey).toString());

    QLabel *sourceLabel = NEW_AS_QT_CHILD(QLabel, this);
    DBG_WARN_AND_RETURN_VOID_UNLESS(sourceLabel, L"sourceLabel为空。",L"GDMPLab",L"2024-03-30");
    sourceLabel->setObjectName(QStringLiteral("labelMainText"));
    sourceLabel->setText(sourceText);
    connect(sourceLabel, SIGNAL(linkActivated(QString)), this, SLOT(openUrl(QString)));
    layout->addWidget(sourceLabel);

    // LICENSE.GPLv3
    QString gplLabelText = settings.value(commonSectionName + "/GPLText").toString().arg(exePath + "//LICENSE.GPLv3.txt");
    QLabel *GPLLabel = NEW_AS_QT_CHILD(QLabel, this);
    DBG_WARN_AND_RETURN_VOID_UNLESS(GPLLabel, L"GPLLabel为空。",L"GDMPLab",L"2024-03-30");
    GPLLabel->setObjectName(QStringLiteral("labelMainText"));
    GPLLabel->setText(gplLabelText);
    connect(GPLLabel, SIGNAL(linkActivated(QString)), this, SLOT(showGPL()));
    layout->addWidget(GPLLabel);

    // LICENSE.LGPLv3
    QString lgplLabelText = settings.value(commonSectionName + "/LGPLText").toString().arg(exePath + "//LICENSE.LGPLv3.txt");
    QLabel *LGPLLabel = NEW_AS_QT_CHILD(QLabel, this);
    DBG_WARN_AND_RETURN_VOID_UNLESS(LGPLLabel, L"LGPLLabel为空。",L"GDMPLab",L"2024-03-30");
    LGPLLabel->setObjectName(QStringLiteral("labelMainText"));
    LGPLLabel->setText(lgplLabelText);
    connect(LGPLLabel, SIGNAL(linkActivated(QString)), this, SLOT(showLGPL()));
    layout->addWidget(LGPLLabel);

    setMinimumWidth(500);
}

void AboutQtDialog::openUrl(QString strUrl)
{
    if (QUrl(strUrl).isValid())
    {
        QDesktopServices::openUrl(QUrl(strUrl));
    }
}

void AboutQtDialog::showGPL()
{
    QString exePath;
    GetConfigFilePath(exePath);
    QDesktopServices::openUrl(QUrl::fromLocalFile(QString("%1/LICENSE.GPLv3.txt").arg(exePath)));
}

void AboutQtDialog::showLGPL()
{
    QString exePath;
    GetConfigFilePath(exePath);
    QDesktopServices::openUrl(QUrl::fromLocalFile(QString("%1/LICENSE.LGPLv3.txt").arg(exePath)));
}
